﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QConnectSDK;

namespace OpenConnect.WebSample.Photo
{
    public partial class UploadPic : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var qzone = Session["QzoneOauth"] as QOpenClient;
            if (qzone != null)
            {
                string desc = this.photodesc.Text;
                string title = this.title.Text;
                int x = Convert.ToInt32(this.x.Text);
                int y = Convert.ToInt32(this.y.Text);
                string albumid = this.albumid.Text;

                bool fileOK = false;
                string path = Server.MapPath("~/App_Data/Temp");
                if (FileUpload1.HasFile)
                {
                    String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                    String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" };
                    for (int i = 0; i < allowedExtensions.Length; i++)
                    {
                        if (fileExtension == allowedExtensions[i])
                        {
                            fileOK = true;
                        }
                    }
                }
                if (fileOK)
                {
                    try
                    {
                        FileUpload1.SaveAs(path + FileUpload1.FileName);
                        var result = qzone.UploadPic(desc, title, albumid,
                               x, y, FileUpload1.PostedFile.FileName, FileUpload1.FileBytes);

                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }
    }
}